/*=======================================================================================
	AverageMarginalTaxRate.do
	
		Estimates the average marginal tax rate payed on the last $ of PFD received
		by all households in the sample.
		
		Note: Barro and Redlick(2011) estimate AMTRs for 2003-2006 of about 21.4%,
		      Mertens (2013) estiamtes the AMTR for 2010 to be around 22.1%-23.5%
		
	Author: Lorenz Kueng, June 2015
=========================================================================================*/

cap log close PFW_08
log using "$homedir/log-files/PFW_08_$date.log", text replace name(PFW_08)


*=============================================================
* Data 
*=============================================================
	
use "$homedir/data/stata/PFW_monthly.dta", clear

	cap drop date
	generate date = Month


	***Keep only Alaskans that receive full dividend
	
	keep if APFDid_FullAmount_annual!=.
	

	***Allocate previous year's dividend to current year's tax payments
	
	bysort userid year: egen APFDannualAK = total(APFD)
	
	drop if year<2010 // we don't observe the 2010 dividend

	preserve
		keep userid year APFDannualAK
		duplicates drop userid year, force
		replace year = year+1 // assign current year's dividend to next year's tax payments
		sort userid year
		tempfile tax
		save `tax'
	restore
	
	drop APFDannualAK
	merge m:1 userid year using `tax'
	drop if _merge==2
	drop _merge
	
	replace APFDannualAK=. if APFD_annual==. 
	

	* Month dummies and month-by-previous-dividend dummies
	
	forvalues m = 1(1)12 {
	
		cap drop mm`m'
		generate mm`m' = month==`m'
		lab var  mm`m' "I(month `m')"
		
		cap drop m`m'd
		generate m`m'd = mm`m' * APFDannualAK
		lab var  m`m'd "I(month `m') x previous PFD"
	}
	
	xtset userid date



*=============================================================
* Estimate federal AMTR 
*=============================================================

	global file "$homedir/results/tables/AMTR_$date"
	cap rm "${file}.xls"
	cap rm "${file}.txt"

	generate cat1901_19 = cat1901+cat19
	lab var  cat1901_19 "federal taxes, including uncategorized taxes"

	foreach depvar in cat1901_19 {	

		global depvar = "`depvar'"
	 
		cap log close AMTR_${depvar}
		log using "$homedir/results/figures/AMTR_${depvar}_$date.log", text replace name(AMTR_${depvar})
		
		*----------------------------------------------
		* Estimate AMTR
		*----------------------------------------------
		
		reg ${depvar} m*d mm*, nocons cluster(userid) 
		
		coefplot, keep(m*d) vertical xtitle("month") xlabel(1(1)12) ///
			yline(0, lp(solid) lc(black) lw(thin)) yscale(r(0)) ylabel(-0.02(0.02)0.12) ///
			mlabel format(%-3.2f) mlabc(black) ciopts(recast(rcap) lc(black)) mcolor(black) graphregion(color(white) lstyle(none))
		
			local file "$homedir/results/figures/AMTR_${depvar}"
				graph export "`file'.eps", replace
				cap rm       "`file'.pdf"
				!epstopdf    "`file'.eps"
				rm           "`file'.eps"
				graph export "`file'.tif", replace
				
		
		* total effect on taxes 
		
		test m1d + m2d + m3d + m4d + m5d + m6d + m7d + m8d + m9d + m10d + m11d + m12d = 0
		local beta_agg_1to12 = _b[m1d] + _b[m2d] + _b[m3d] + _b[m4d] + _b[m5d] + _b[m6d] + _b[m7d] + _b[m8d] + _b[m9d] + _b[m10d] + _b[m11d] + _b[m12d] 
		local se_agg_1to12 = `beta_agg_1to12'/sqrt(r(F))
		local t_agg_1to12 = abs(`beta_agg_1to12'/`se_agg_1to12')
		
		di "beta: aggregate 1 to 12"
		di `beta_agg_1to12'
		di "SE: aggregate 1 to 12"
		di   `se_agg_1to12'
		di "t-stat: aggregate 1 to 12"
		di    `t_agg_1to12'
		di "p-value: aggregate 1 to 12"
		di `r(p)'

		outreg2 using "${file}.xls", alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters,e(N_clust), ///
			sum beta(M1) to beta(M12), `beta_agg_1to12', ///
			SE,                          `se_agg_1to12', /// 
			t stat,                       `t_agg_1to12', ///
			p value,                      `r(p)' ///
			) ///
			addtext( ///
				"Month FEs", YES ///
			) ///
			keep( m*d )

		cap log close AMTR_${depvar}
	}
	cap rm "${file}.txt"

log close PFW_08
